package com.github.lzyzsd.circleprogress.demo;

import com.github.lzyzsd.circleprogress.ArcProgress;
import com.github.lzyzsd.circleprogress.CircleProgress;
import com.github.lzyzsd.circleprogress.DonutProgress;
import com.github.lzyzsd.circleprogress.utils.LogUtil;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ListAbility extends Ability {
    public static final String TAG = ListAbility.class.getSimpleName();

    private static class Triple {
        private int type;
        private int progress;
        public String name;
    }

    private List<Triple> list = new ArrayList<>();

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_layout_list_container);
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_list);
        Random random = new Random(27);
        for (int i = 0; i < 30; i++) {
            Triple triple = new Triple();
            triple.type = random.nextInt(3);
            triple.progress = random.nextInt(100);
            list.add(triple);
        }

        listContainer.setItemProvider(new BaseItemProvider() {
            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public Object getItem(int i) {
                return list.get(i);
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
                ViewHolder vh = null;
                if (component == null) {
                    System.out.println(TAG + " position:" + i + "  the component is null");
                    LogUtil.error(TAG, "position:" + i + "  the component is null");
                    component = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_items_progress, componentContainer, false);
                    vh = new ViewHolder();
                    vh.findViews(component);
                } else {
                    vh = (ViewHolder) component.getTag();
                }

                Triple triple = (Triple) getItem(i);
                vh.makeVisibleBy(triple.type);
                vh.setProgress(triple.progress);
                return component;
            }
        });
    }

    private class ViewHolder {
        public CircleProgress circleProgress;
        public ArcProgress arcProgress;
        public DonutProgress donutProgress;
        public Text progress;
        public Text maxprogress;

        public void findViews(Component component) {
            arcProgress = (ArcProgress) component.findComponentById(ResourceTable.Id_arc);
            circleProgress = (CircleProgress) component.findComponentById(ResourceTable.Id_circle);
            donutProgress = (DonutProgress) component.findComponentById(ResourceTable.Id_donut);
            progress = (Text) component.findComponentById(ResourceTable.Id_text_one);
            maxprogress = (Text) component.findComponentById(ResourceTable.Id_text_two);
            component.setTag(this);
        }

        public void setProgress(int p) {
            arcProgress.setProgress(p);
            circleProgress.setProgress(p);
            donutProgress.setProgress(p);
            progress.setText("progress: " + p);
            maxprogress.setText("max progress: 100");
        }

        public void makeVisibleBy(int type) {
            switch (type) {
                case 0:
                    makeArcVisible();
                    break;
                case 1:
                    makeCircleVisible();
                    break;
                case 2:
                    makeDonutVisible();
                    break;

            }
        }

        private void makeArcVisible() {
            arcProgress.setVisibility(Component.VISIBLE);
            circleProgress.setVisibility(Component.HIDE);
            donutProgress.setVisibility(Component.HIDE);
        }

        private void makeCircleVisible() {
            arcProgress.setVisibility(Component.HIDE);
            circleProgress.setVisibility(Component.VISIBLE);
            donutProgress.setVisibility(Component.HIDE);
        }

        private void makeDonutVisible() {
            arcProgress.setVisibility(Component.HIDE);
            circleProgress.setVisibility(Component.HIDE);
            donutProgress.setVisibility(Component.VISIBLE);
        }
    }
}
